<%--
  Created by IntelliJ IDEA.
  com.baizhi.entity.User: Administrator
  Date: 2021/1/26
  Time: 11:13
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form action="" method="post">
        用户名:<input type="text" name="username" onblur="doajax()"/><span id="msg"></span>
    </form>
    <script type="text/javascript">
        function doajax(){
            //做ajax
            //1.创建核心对象xhr
            let xhr=new XMLHttpRequest();
            //2.指定请求路径,携带数据
            let username=document.querySelector("[name=username]").value;
            xhr.open("get","${pageContext.request.contextPath}/user/check.do?username="+username);
            //3.发出请求
            xhr.send();
            //4.设置回调函数
            xhr.onreadystatechange=function(){
                //判断状态是否是4,status是否是200
                /*readyState:0,1,2,3,4
                    0:xhr对象刚被创建
                    1:发出请求
                    2:服务器端接收到请求.
                    3:服务器端开始响应.
                    4:浏览器接收到响应的数据.
                 */
                /*status:200,400,404,500
                    200:服务器端正常响应
                    400:请求的参数错误
                    404:请求路径不存在
                    500:服务器内部异常.
                 */
                if(xhr.readyState==4&&xhr.status==200){
                    let result=xhr.responseText;//获取服务器响应的数据
                    if(result=="true"){
                        document.querySelector("#msg").innerHTML="该用户名已经被注册";
                    }else{
                        document.querySelector("#msg").innerHTML="该用户名可以注册";
                    }
                }
            }
        }
    </script>
</body>
</html>
